package com.charm.businesshub.contract.presenter

import com.charm.businesshub.api.HubServiceManager
import com.charm.businesshub.contract.NewsListView
import com.charm.businesshub.entity.NewsListItemEntity
import com.charm.codes.lifecycle.Live
import com.charm.codes.mvp.presenter.BaseMvpPresenter
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * Created by tools on 2018/1/5.
 */
class NewsListPresenter : BaseMvpPresenter<NewsListView>() {

    fun getNewsList() {
        var kw = "白"
        var site = "qq.com"
        var apikey = "MqtghNHt2aLFogjaxX0uL0sx5SncuWTnR4cqc7UgcUoYU0g7v5zQjVZOjbCo4BAu"
        HubServiceManager.hubService()!!.newsList(kw, site, apikey)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(Live.bindLifecycle(mvpView))
                .filter { t -> t.data != null }
                .map { t -> t.data!! }
                .subscribe(object : Observer<List<NewsListItemEntity>> {
                    override fun onError(e: Throwable) {
                    }

                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: List<NewsListItemEntity>) {
                        mvpView.newsList(t)
                    }

                })

    }
}